package com.xbongbong.saas.help.workflow;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.product.pojo.ProductSavePojo;
import com.xbongbong.saas.domain.entity.ContractProductEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.ContractProductModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 *
 */
@Component
public class RefundSaveHelp {

    @Resource
    private ContractProductModel contractProductModel;

    public boolean judgeRefundAllInBound(Long instockId, Long refundId, String corpid, List<ProductSavePojo> productSavePojoList) throws XbbException {
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("contractId", refundId);
        param.put("type", XbbRefTypeEnum.REFUND.getCode());
        param.put("del", 0);
        // 拿到合同的所有产品
        List<ContractProductEntity> contractProductEntityList = contractProductModel.findEntitys(param);
//        Map<Long,Double> contractProductMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//        Map<Long,Double> lineMatchPurchaseProductNum = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Double> refundNumMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<ContractProductEntity> updateList = new ArrayList<>();
        productSavePojoList.forEach(item -> {
            if (Objects.nonNull(item.getRefProductId())) {
                Double num = refundNumMap.getOrDefault(item.getRefProductId(), 0D);
                num = Arith.add(num, item.getNum());
                refundNumMap.put(item.getRefProductId(), num);
            }
        });
        boolean isAllOutBound = true;
        for (ContractProductEntity entity : contractProductEntityList){
            Long key = entity.getId();
            if (refundNumMap.containsKey(key)) {
                Double instockNum = entity.getInstockNum();
                // 当前入库
                Double amendNum = refundNumMap.get(key);
                entity.setInstockNum(amendNum);
                // 只要有一个入库数量 < 产品总数，就应该是未全部入库
                if (Arith.sub(Math.abs(entity.getProductNum()) , Arith.add(instockNum, amendNum)) > 0) {
                    isAllOutBound = false;
                }
                entity.setUpdateTime(DateTimeUtil.getInt());
                updateList.add(entity);
            }else {
                //该退货退款产品不在入库单中，退货退款肯定没入完
                if (Arith.sub(Math.abs(entity.getProductNum()) , entity.getInstockNum()) > 0) {
                    isAllOutBound = false;
                }
            }
//            Double num = contractProductMap.getOrDefault(entity.getProductId(),0D);
//            entity.setProductNum(Math.abs(entity.getProductNum()));
//            Double productNum = entity.getProductNum() == null ? 0D : Math.abs(entity.getProductNum());
//            num += productNum;
//            contractProductMap.put(entity.getProductId(),num);
//            lineMatchPurchaseProductNum.put(entity.getId(), entity.getProductNum());
//            if (!refundNumMap.isEmpty()){
//                if (refundNumMap.containsKey(entity.getId())) {
//                    Double instockNum = Objects.isNull(entity.getInstockNum()) ? 0D : entity.getInstockNum();
//                    entity.setInstockNum(Arith.add(instockNum, refundNumMap.get(entity.getId())));
//                    updateList.add(entity);
//                }
//            }
        }
        if(!updateList.isEmpty()){
            contractProductModel.updateInStock(updateList,corpid);
        }
        return isAllOutBound;
    }
}
